﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ChiantiTestLibrary;

namespace Chianti.Test.Service
{
    public class ServiceTestLibrary
    {
        public static double ConvertPositionToDecimal(int degrees, double minutes, double seconds)
        {
            double result =  (Math.Abs(degrees)) + (minutes * 1 / 60) + (seconds * 1 / 3600);
            if (degrees < 0)
            {
                return result * -1;
            }
            return result;
        }

        public static bool CreateDealTest1()
        {
            bool result = false;
            long businessID = 0;
            long dealID = 0;

            try
            {
                businessID = CreateSampleBusiness(ConvertPositionToDecimal(40, 45, 21), ConvertPositionToDecimal(73, 59, 52));
                dealID = CreateSampleDeal(businessID, DateTime.Now.AddDays(10), 1.00, 10);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                DeleteBusiness(businessID);
            }
            return result;
        }
        public static bool CreateDealTest2()
        {
            long businessID1=0;
            long businessID2=0;
            long businessID3=0;
            long dealID1=0;
            long dealID2=0;
            long dealID3=0;
            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(0.0,0.0);
                businessID2 = CreateSampleBusiness(90,0);
                businessID3 = CreateSampleBusiness(-90,0);

                dealID1 = CreateSampleDeal(businessID1, DateTime.Now.AddDays(3), 0, 0);
                dealID2 = CreateSampleDeal(businessID2, DateTime.Now.AddDays(3), 0, 0);
                dealID3 = CreateSampleDeal(businessID3, DateTime.Now.AddDays(3), 0, 0);
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);
                DeleteBusiness(businessID3);
            }
            return result;
        }
        public static bool CreateDealTest3()
        {
            long businessID1 = 0;
            long businessID2 = 0;
            long businessID3 = 0;
            long dealID1 = 0;
            long dealID2 = 0;
            long dealID3 = 0;
            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(0.0, 0.0);
                businessID2 = CreateSampleBusiness(90, 0);
                businessID3 = CreateSampleBusiness(-90, 0);

                dealID1 = CreateSampleDeal(businessID1, DateTime.Now.AddDays(3), 0, 0);
                dealID2 = CreateSampleDeal(businessID2, DateTime.Now.AddDays(3), 0, 0);
                dealID3 = CreateSampleDeal(businessID3, DateTime.Now.AddDays(3), 0, 0);

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 0, 500);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 0, 500);
                if (response2.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response3 = UpdateDealBid(dealID3, 0, 500);
                if (response3.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);
                DeleteBusiness(businessID3);
            }
            return result;
        }
        public static bool CreateDealTest4()
        {
            long businessID1 = 0;
            long businessID2 = 0;
            long businessID3 = 0;
            long dealID1 = 0;
            long dealID2 = 0;
            long dealID3 = 0;
            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(0.0, 0.0);
                businessID2 = CreateSampleBusiness(90, 0);
                businessID3 = CreateSampleBusiness(-90, 0);

                dealID1 = CreateSampleDeal(businessID1, DateTime.Now.AddDays(3), 0, 0);
                dealID2 = CreateSampleDeal(businessID2, DateTime.Now.AddDays(3), 0, 0);
                dealID3 = CreateSampleDeal(businessID3, DateTime.Now.AddDays(3), 0, 0);

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 12000, 500);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 0, 500);
                if (response2.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response3 = UpdateDealBid(dealID3, 0, 500);
                if (response3.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);
                DeleteBusiness(businessID3);
            }
            return result;
        }
        public static bool CreateDealTest5()
        {
            long businessID1 = 0;
            long businessID2 = 0;
            long businessID3 = 0;
            long dealID1 = 0;
            long dealID2 = 0;
            long dealID3 = 0;
            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(0.0, 0.0);
                businessID2 = CreateSampleBusiness(90, 0);
                businessID3 = CreateSampleBusiness(-90, 0);

                dealID1 = CreateSampleDeal(businessID1, DateTime.Now.AddDays(3), 0, 0);
                dealID2 = CreateSampleDeal(businessID2, DateTime.Now.AddDays(3), 0, 0);
                dealID3 = CreateSampleDeal(businessID3, DateTime.Now.AddDays(3), 0, 0);

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 1, 12000000);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 0, 0);
                if (response2.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response3 = UpdateDealBid(dealID3, 0, 0);
                if (response3.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }

                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);
                DeleteBusiness(businessID3);
            }
            return result;
        }
        public static bool CreateDealTest6()
        {
            long businessID1 = 0;
            long businessID2 = 0;
            long businessID3 = 0;
            long dealID1 = 0;
            long dealID2 = 0;
            long dealID3 = 0;
            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(47.616217, -122.349458); //2nd and cedar
                businessID2 = CreateSampleBusiness(47.615118, -122.347612); //2nd and wall

                //Distance between the two is roughly .1846 km
                

                dealID1 = CreateSampleDeal(businessID1, DateTime.Now.AddDays(3), 1, 1000);
                dealID2 = CreateSampleDeal(businessID2, DateTime.Now.AddDays(3), 0, 100);
             

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 1, 12000000);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 0, 0);
                if (response2.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }


                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);
               
            }
            return result;
        }
        public static bool CreateDealTest7()
        {
            long id_business2 = 0;
            long id_business3 = 0;
            
            long dealID1 = 0;
            long dealID2 = 0;
           
            bool result = false;

            try
            {
                id_business2 = CreateSampleBusiness(47.616217, -122.349458); //2nd and cedar
                id_business3 = CreateSampleBusiness(47.615118, -122.347612); //2nd and wall

                //Distance between the two is roughly .188 km
                dealID1 = CreateSampleDeal(id_business2, DateTime.Now.AddDays(3), 0, 0);
                dealID2 = CreateSampleDeal(id_business3, DateTime.Now.AddDays(3), 0, 0);

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 1, 190);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 0, 50);
                if (response2.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }


                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                DeleteBusiness(id_business2);
                DeleteBusiness(id_business3);

            }
            return result;
        }
        public static bool CreateDealTest8()
        {
            long businessID1 = 0;
            long businessID2 = 0;

            long dealID1 = 0;
            long dealID2 = 0;

            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(47.616217, -122.349458); //2nd and cedar
                businessID2 = CreateSampleBusiness(47.615118, -122.347612); //2nd and wall

                //Distance between the two is roughly .1846 km
                dealID1 = CreateSampleDeal(businessID1, DateTime.Now.AddDays(3), 0, 0);
                dealID2 = CreateSampleDeal(businessID2, DateTime.Now.AddDays(3), 0, 0);


                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 1, 93);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 0, 93);
                if (response2.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }


                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);

            }
            return result;
        }

        public static bool ExpirationTest1()
        {
            long businessID1 = 0;
            long businessID2 = 0;

            long dealID1 = 0;
            long dealID2 = 0;

            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(47.616217, -122.349458); //2nd and cedar
                businessID2 = CreateSampleBusiness(47.615118, -122.347612); //2nd and wall

                //Distance between the two is roughly .1846 km
                dealID1 = CreateSampleDeal(businessID1, DateTime.Now.AddDays(-3), 0, 0);
                dealID2 = CreateSampleDeal(businessID2, DateTime.Now.AddDays(3), 0, 0);


                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 1, 200);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 0, 93);
                if (response2.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }


                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);

            }
            return result;
        }
        public static bool ExpirationTest2()
        {
            long businessID1 = 0;
            long businessID2 = 0;

            long dealID1 = 0;
            long dealID2 = 0;

            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(47.616217, -122.349458); //2nd and cedar
                businessID2 = CreateSampleBusiness(47.615118, -122.347612); //2nd and wall

                //Distance between the two is roughly .1846 km

                DateTime deal1ExpirationTime = DateTime.Now.AddSeconds(30);
                dealID1 = CreateSampleDeal(businessID1, deal1ExpirationTime, 0, 0);
                dealID2 = CreateSampleDeal(businessID2, DateTime.Now.AddDays(3), 0, 0);


                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 2, 200);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 0, 93);
                if (response2.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }

                //Wait for 30 seconds
                Thread.Sleep(1000 * 45);
                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response3 = UpdateDealBid(dealID2, 1, 93);
                if (response3.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }
                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);

            }
            return result;
        }

        public static bool ExpirationTest3()
        {
            long businessID1 = 0;
            long businessID2 = 0;
            long businessID3 = 0;
            long businessID4 = 0;

            long dealID1 = 0;
            long dealID2 = 0;
            long dealID3 = 0;
            long dealID4 = 0;

            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(ConvertPositionToDecimal(47, 37, 5.79), ConvertPositionToDecimal(-122, 21, 10.11),"Radio KIRO", "1 Denny Way", "Seattle", "Washington", "98101");  //2nd and denny
                businessID2 = CreateSampleBusiness(ConvertPositionToDecimal(47, 36, 58.33), ConvertPositionToDecimal(-122, 20, 58.07),"Indian Restaurant", "9600 2nd Avenue", "Seattle", "Washington", "98101"); //2nd and cedar
                businessID3 = CreateSampleBusiness(ConvertPositionToDecimal(47,36,54.19),ConvertPositionToDecimal(-122,20,51.39),"Buckley's Restaurant","34 Wall Street", "Seattle", "Washington", "98101");  //2nd and wall
                businessID4 = CreateSampleBusiness(ConvertPositionToDecimal(47,36,47.68),ConvertPositionToDecimal(-122,20,39.80), "Mama Rosas", "23 Blanchard Street", "Seattle", "Washington", "98101");  //2nd and blanchard
                
                

                DateTime deal3ExpirationTime = DateTime.Now.AddSeconds(30);
                dealID1 = CreateSampleDeal(businessID1, DateTime.Now.AddDays(3), 0, 0,"Receive one free showing of KIRO's news desk", "KIRO Newsdesk Tour");
                dealID2 = CreateSampleDeal(businessID2, DateTime.Now.AddDays(3), 0, 0,"Buy 2 get 3 free indian dessert", "Indian desert special");
                dealID3 = CreateSampleDeal(businessID3, deal3ExpirationTime, 0, 0,"Free Chicken Wings at Dinner","Free Chicken Wings with Dinner");
                dealID4 = CreateSampleDeal(businessID4, DateTime.Now.AddDays(3), 0, 0);

                //B1->B2: 355 meters
                //B2->B3: 188 meters
                //B3->B4: 332 meters
                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 2, 210);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 1, 150);
                if (response2.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }

    
                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response3 = UpdateDealBid(dealID3, 2, 100);
                if (response3.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response4 = UpdateDealBid(dealID4, 1, 250);
                if (response4.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }

                //Wait for 30 seconds for deal 3 to expire
                Thread.Sleep(32000);
                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response5 = UpdateDealBid(dealID4, 1, 250);
                if (response5.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }

            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);
                DeleteBusiness(businessID3);
                DeleteBusiness(businessID4);

            }
            return result;
        }


        /// <summary>
        /// This test will create 4 deals, each overlapping, with staggered expirations. After each deal expires, the next deal should be promoted to active.
        /// </summary>
        /// <returns></returns>
        public static bool ExpirationTest4()
        {
            long businessID1 = 0;
            long businessID2 = 0;
            long businessID3 = 0;
            long businessID4 = 0;

            long dealID1 = 0;
            long dealID2 = 0;
            long dealID3 = 0;
            long dealID4 = 0;

            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(ConvertPositionToDecimal(47, 37, 5.79), ConvertPositionToDecimal(-122, 21, 10.11), "Radio KIRO", "1 Denny Way", "Seattle", "Washington", "98101");  //2nd and denny
                businessID2 = CreateSampleBusiness(ConvertPositionToDecimal(47, 37, 5.79), ConvertPositionToDecimal(-122, 21, 9.24), "Business 2", "1 Denny Way", "Seattle", "Washington", "98101");  //2nd and denny
                businessID3 = CreateSampleBusiness(ConvertPositionToDecimal(47, 37, 4.88), ConvertPositionToDecimal(-122, 21, 8.46), "Business 3", "1 Denny Way", "Seattle", "Washington", "98101");  //2nd and denny
                businessID4 = CreateSampleBusiness(ConvertPositionToDecimal(47, 37, 4.18), ConvertPositionToDecimal(-122, 21, 7.14), "Business 4", "1 Denny Way", "Seattle", "Washington", "98101");  //2nd and denny
               


                DateTime deal1ExpirationTime = DateTime.Now.AddSeconds(30);
                dealID1 = CreateSampleDeal(businessID1, deal1ExpirationTime, 0, 100, "Receive one free showing of KIRO's news desk", "KIRO Newsdesk Tour");
                dealID2 = CreateSampleDeal(businessID2, deal1ExpirationTime.AddSeconds(30), 0, 100, "Buy 2 get 3 free indian dessert", "Indian desert special");
                dealID3 = CreateSampleDeal(businessID3, deal1ExpirationTime.AddSeconds(60), 0, 100, "Free Chicken Wings at Dinner", "Free Chicken Wings with Dinner");
                dealID4 = CreateSampleDeal(businessID4, deal1ExpirationTime.AddSeconds(110), 0, 100);

                //B1->B2: 355 meters
                //B2->B3: 188 meters
                //B3->B4: 332 meters
                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 7, 1000);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 6, 1000);
                if (response2.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }


                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response3 = UpdateDealBid(dealID3, 5, 1000);
                if (response3.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response4 = UpdateDealBid(dealID4, 4, 1000);
                if (response4.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.Position currentLongitude = new ChiantiTestLibrary.ChiantiServiceReference.Position();
                ChiantiTestLibrary.ChiantiServiceReference.Position currentLatitude = new ChiantiTestLibrary.ChiantiServiceReference.Position();
                
                currentLongitude.Degrees = 122;
                currentLongitude.Minutes = 21;
                currentLongitude.Seconds=12.15;

                currentLatitude.Degrees = 47;
                currentLatitude.Minutes = 37;
                currentLatitude.Seconds = 4.78;

                //Wait for 30 seconds for deal 1 to expire
                Thread.Sleep(32000);

                ChiantiTestLibrary.ChiantiServiceReference.Deal[] deals;
                using (ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient client = new ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient())
                {
                     deals = client.FindActiveDeals(currentLongitude, currentLatitude, 10000, 100);
                }

                if (deals.Count() != 1)
                {
                    throw new Exception();
                }

                if (deals[0].ID != dealID2)
                {
                    throw new Exception();
                }

                //Wait for 30 seconds for deal 2 to expire
                deals = null;
                Thread.Sleep(32000);
                using (ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient client = new ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient())
                {
                    deals = client.FindActiveDeals(currentLongitude, currentLatitude, 10000, 100);
                }

                if (deals.Count() != 1)
                {
                    throw new Exception();
                }

                if (deals[0].ID != dealID3)
                {
                    throw new Exception();
                }

                //Wait for 30 seconds for deal 3 to expire
                deals = null;
                Thread.Sleep(32000);
                using (ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient client = new ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient())
                {
                    deals = client.FindActiveDeals(currentLongitude, currentLatitude, 10000, 100);
                }

                if (deals.Count() != 1)
                {
                    throw new Exception();
                }

                if (deals[0].ID != dealID4)
                {
                    throw new Exception();
                }

                //Wait for 30 seconds for deal 4 to expire
                deals = null;
                Thread.Sleep(32000);
                using (ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient client = new ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient())
                {
                    deals = client.FindActiveDeals(currentLongitude, currentLatitude, 10000, 100);
                }

                if (deals.Count() != 0)
                {
                    throw new Exception();
                }


                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }

            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);
                DeleteBusiness(businessID3);
                DeleteBusiness(businessID4);

            }
            return result;
        }

        public static void DeleteBusiness(long businessID)
        {
            using (ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient client = new ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient())
            {
                client.DeleteBusiness(businessID);
            }
        }


        /// <summary>
        /// This test will create the same scenario as Test 5 however not run it. Its used to help debug the phone app.
        /// </summary>
        /// <returns></returns>
        public static bool ExpirationTest5()
        {
            long businessID1 = 0;
            long businessID2 = 0;
            long businessID3 = 0;
            long businessID4 = 0;

            long dealID1 = 0;
            long dealID2 = 0;
            long dealID3 = 0;
            long dealID4 = 0;

            bool result = false;

            try
            {
                businessID1 = CreateSampleBusiness(ConvertPositionToDecimal(47, 37, 5.79), ConvertPositionToDecimal(-122, 21, 10.11), "Radio KIRO", "1 Denny Way", "Seattle", "Washington", "98101");  //2nd and denny
                businessID2 = CreateSampleBusiness(ConvertPositionToDecimal(47, 37, 5.79), ConvertPositionToDecimal(-122, 21, 9.24), "Business 2", "1 Denny Way", "Seattle", "Washington", "98101");  //2nd and denny
                businessID3 = CreateSampleBusiness(ConvertPositionToDecimal(47, 37, 4.88), ConvertPositionToDecimal(-122, 21, 8.46), "Business 3", "1 Denny Way", "Seattle", "Washington", "98101");  //2nd and denny
                businessID4 = CreateSampleBusiness(ConvertPositionToDecimal(47, 37, 4.18), ConvertPositionToDecimal(-122, 21, 7.14), "Business 4", "1 Denny Way", "Seattle", "Washington", "98101");  //2nd and denny



                DateTime deal1ExpirationTime = DateTime.Now.AddSeconds(30);
                dealID1 = CreateSampleDeal(businessID1, deal1ExpirationTime, 0, 100, "Receive one free showing of KIRO's news desk", "KIRO Newsdesk Tour");
                dealID2 = CreateSampleDeal(businessID2, deal1ExpirationTime.AddSeconds(30), 0, 100, "Buy 2 get 3 free indian dessert", "Indian desert special");
                dealID3 = CreateSampleDeal(businessID3, deal1ExpirationTime.AddSeconds(60), 0, 100, "Free Chicken Wings at Dinner", "Free Chicken Wings with Dinner");
                dealID4 = CreateSampleDeal(businessID4, deal1ExpirationTime.AddSeconds(110), 0, 100);

                //B1->B2: 355 meters
                //B2->B3: 188 meters
                //B3->B4: 332 meters
                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response1 = UpdateDealBid(dealID1, 7, 1000);
                if (response1.CompetingBids.Count() != 0)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response2 = UpdateDealBid(dealID2, 6, 1000);
                if (response2.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }


                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response3 = UpdateDealBid(dealID3, 5, 1000);
                if (response3.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }

                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response4 = UpdateDealBid(dealID4, 4, 1000);
                if (response4.CompetingBids.Count() != 1)
                {
                    throw new Exception();
                }


                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }

            finally
            {
                DeleteBusiness(businessID1);
                DeleteBusiness(businessID2);
                DeleteBusiness(businessID3);
                DeleteBusiness(businessID4);

            }
            return result;
        }


        public static void DeleteDeal(long dealID)
        {
            using (ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient client = new ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient())
            {
                client.DeleteDeal(dealID);
            }
        }

        public static long CreateSampleBusiness(double latitude, double longitude, string name, string streetaddress, string city, string state, string zipcode)
        {
            DateTime currentDate = DateTime.Now;
            ChiantiTestLibrary.ChiantiServiceReference.Business newBusiness = new ChiantiTestLibrary.ChiantiServiceReference.Business();
            newBusiness.City = city;
            newBusiness.State = state;
            newBusiness.Name = name ;
            newBusiness.StreetAddress = streetaddress;
            newBusiness.Latitude = latitude;
            newBusiness.Longitude = longitude;
            newBusiness.ZipCode = Convert.ToInt32(zipcode);

            using (ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient client = new ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient())
            {
                return client.CreateBusiness(newBusiness);
            }
        }
        public static long CreateSampleBusiness(double latitude, double longitude)
        {
            DateTime currentDate = DateTime.Now;
            return CreateSampleBusiness(latitude, longitude, currentDate.ToString() + ": Sample Business", "23 dicks avenue","seattle","wa", "10018");
        }

        public static long CreateSampleDeal(long businessID, DateTime expiryDate, double bidAmount, int radiusInMeters, string description, string title)
        {
            ChiantiTestLibrary.ChiantiServiceReference.Deal newDeal = new ChiantiTestLibrary.ChiantiServiceReference.Deal();
            newDeal.BusinessID = businessID;
            newDeal.CreationDate = DateTime.Now;
            newDeal.Description = description;
            newDeal.Title = title;
            newDeal.ExpiryDate = expiryDate;
            newDeal.IsActive = true;

            using (ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient client = new ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient())
            {
                ChiantiTestLibrary.ChiantiServiceReference.CreateDealResponse response = client.CreateDeal(newDeal, bidAmount, radiusInMeters);
                return response.DealID;
            }


        }
        public static long CreateSampleDeal(long businessID, DateTime expiryDate, double bidAmount, int radiusInMeters)
        {
            DateTime currentDate = DateTime.Now;
            return CreateSampleDeal(businessID, expiryDate, bidAmount, radiusInMeters, currentDate.ToString() + ": sample deal description", currentDate.ToString() + ": sample deal title");

             
        }

        public static ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse UpdateDealBid(long dealId, double newBidAmount, int radiusInMeters)
        {

            using (ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient client = new ChiantiTestLibrary.ChiantiServiceReference.DealManagementServiceClient())
            {
                ChiantiTestLibrary.ChiantiServiceReference.UpdateDealBidResponse response = client.UpdateDealBid(dealId, newBidAmount, radiusInMeters);
                return response;
            }
        }
    }
}
